﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Schema;
using Photon.SocketServer;
using WorldOfSuperpowers_COMMON;
using WorldOfSuperpowers_COMMON.DataModel;

namespace WorldOfSuperpowers.Cache
{
    public class UserCache
    {
        public static Dictionary<string,UserDataModel> UserDataDicCache = new Dictionary<string, UserDataModel>();
        public static Dictionary<string, MasterClientPeer> AllConnectedPeerDic = new Dictionary<string, MasterClientPeer>(); 
        public static List<string> AllPlayersOnLineList = new List<string>();
        public static List<string> UserAccountInLobbyList = new List<string>();
        public static List<string> UserAccountInGameList = new List<string>();

        /// <summary>
        /// 注册新账号
        /// </summary>
        /// <param name="acount"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        public static bool RegistNewUser(ref UserDataModel model)
        {
            //返回true，则是可用注册此账号
            //返回false, 则是没有
            if (!UserDataDicCache.ContainsKey(model.UserAccount))
            {
                //todo 注册新账号
                model.UserLevel = 1;
                model.UserName = "";
                model.IconIndex = 0;
                UserDataDicCache.Add(model.UserAccount, model);
                model.UserID = UserDataDicCache.Count + 66000;
                return true;
            }
            else
            {
                model = null;
                return false;
            }
        }

        /// <summary>
        /// 检测是否能登陆
        /// </summary>
        /// <returns></returns>
        public static bool LoginAction(LoginModel Lmodel,MasterClientPeer  peer,ref OperationResponse op)
        {
            bool NeedRename = false;
            UserDataModel Umodel = new UserDataModel();
            if (UserDataDicCache.TryGetValue(Lmodel.username, out Umodel))
            {
                if (AllConnectedPeerDic.ContainsKey(Umodel.UserAccount))
                {
                    op.DebugMessage = "账号已登录";
                    op.ReturnCode = (byte)ReturnCode.Fail;
                    //todo 强制下线
                }
                else
                {
                    //找到账号，检测密码
                    if (Lmodel.password == Umodel.UserPassword)
                    {
                        if (Umodel.UserName == "")
                            NeedRename = true;
                        op.DebugMessage = "登录成功";
                        UserAccountInLobbyList.Add(Umodel.UserAccount);
                        peer.UserAccount = Umodel.UserAccount;
                        AllPlayersOnLineList.Add(Umodel.UserAccount);
                        AllConnectedPeerDic.Add(Umodel.UserAccount, peer);
                        op.ReturnCode = (byte)ReturnCode.Success;
                        MasterServer.log.Debug("登陆成功,ip:" + peer.RemoteIP + "玩家名字是否为空:" + (Umodel.UserName == ""));
                    }
                    else
                    {
                        op.DebugMessage = "密码错误";
                        op.ReturnCode = (byte)ReturnCode.Fail;
                    }
                }
            }
            else
            {
                //该账号不存在
                op.DebugMessage = "该账号不存在";
                op.ReturnCode = (byte)ReturnCode.Fail;
            }
            Dictionary<byte, object> content = new Dictionary<byte, object>();
            object Umodel_ByteArr = ProtoTranslater.GetInstance().GetBytesFromType(Umodel);
            content.Add((byte)OperationCode.Login, Umodel_ByteArr);
            op.Parameters = content;
            return NeedRename;
        }

        /// <summary>
        /// 是否重命名成功
        /// </summary>
        /// <param name="NewName">命名的名字</param>
        /// <returns></returns>
        public static bool RenameAction(UserDataModel Umodel)
        {
            List<UserDataModel> models = UserDataDicCache.Values.ToList();
            int rezult = models.Where(x => x.UserName == Umodel.UserName).Count();
            if (rezult == 0)
            {
                UserDataDicCache[Umodel.UserAccount].UserName = Umodel.UserName;
                MasterServer.log.Debug("客户端ip:"+AllConnectedPeerDic[Umodel.UserAccount].RemoteIP+",更名:"+ Umodel.UserName);
            }
            return rezult == 0;
        }

        /// <summary>
        /// 从在线列表中移除
        /// </summary>
        /// <param name="UserAccount"></param>
        public static void RemovePlayerForOffline(string UserAccount)
        {
            AllPlayersOnLineList.Remove(UserAccount);
            AllConnectedPeerDic.Remove(UserAccount);
            MatchCenter.GetInstance().RemovePlayerFromQueue(UserAccount);
        }
    }
}
